package jinyilw.swing.visuals.text;

import jinyilw.swing.ComponentTools;
import jinyilw.swing.rlaf.theme.RThemeColor;

import javax.swing.JComponent;
import javax.swing.JTextArea;
import javax.swing.plaf.basic.BasicTextAreaUI;
import javax.swing.text.Document;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class TransTextArea extends JTextArea
{
	private static final long serialVersionUID = -3499591026577247320L;

	public TransTextArea()
	{
	}

	public TransTextArea(String text)
	{
		super(text);
	}

	public TransTextArea(Document doc)
	{
		super(doc);
	}

	public TransTextArea(int rows, int columns)
	{
		super(rows, columns);
	}

	public TransTextArea(String text, int rows, int columns)
	{
		super(text, rows, columns);
	}

	public TransTextArea(Document doc, String text, int rows, int columns)
	{
		super(doc, text, rows, columns);
	}

	@Override
	public void updateUI()
	{
		setUI(new TransTextAreaUI());
		invalidate();
	}

	static class TransTextAreaUI extends BasicTextAreaUI
	{
		JTextArea t;
		@Override
		public void installUI(JComponent c)
		{
			super.installUI(c);
			t = (JTextArea) c;
			t.setOpaque(false);
			t.setSelectionColor(RThemeColor.darkThemeColor);
			t.setSelectedTextColor(RThemeColor.selectedTextColor);
		}

		@Override
		public void update(Graphics g, JComponent c)
		{
			if (t.getSelectionColor() != RThemeColor.darkThemeColor)
				t.setSelectionColor(RThemeColor.darkThemeColor);
			Graphics2D g2d = (Graphics2D) g;
			Composite composite = ComponentTools.applyEnableAlpha(g2d,
					c.isEnabled(), 1f, 0.4f);
			RThemeColor.customForegroundSetting(c);
			paint(g, c);
			if (composite != null)
				g2d.setComposite(composite);
		}
	}
}
